###########################################################################
### Replication Data for: #################################################
### Hemesath, Tepe (2024): Public Value Positions and Design Preferences ##
### toward AI-based Chatbots in e- Government. Evidence from a Conjoint ###
### Experiment with Citizens and Municipal Front Desk Officers. ###########
### Government Information Quarterly.DOI: xxx #############################
###########################################################################

library(tidyverse)
library(cjoint)
library(nnet)
library(quanteda.textstats)
library(tm)
library(wordcloud)

rm(list=ls())

######################
#### Data Preparation ###
#########################

## Load Data

citizen <- readRDS("Citizen_Sample.RDS")
municipalities <- readRDS("Municipalities_Sample.RDS")
contacts <- readRDS("Contacts.RDS")


### Some Labs and Vectors for convenience

ats <- c("Development","Privacy","Takeover","ADM","Workload","Time","Interface","Style")
at_name <- c("Developer","Chat History","Chat Termination","Chatbot decides",
             "Reduction Workload\n(Municipality)","Time Saving\n(Citizens)",
             "Type of Communication","Tone of Communication")

lvl_name <- c("Foreign Company","National Company","Local Company",
              "Always Deleted","Always Saved","Saved with consent",
              "Mail Contact (Municipality)","Mail Contact (Frontline Worker)","Clerk takes over",
              "Including Discretion","Never","Excluding Discretion",
              "14 %","27 %","4%",
              "17 Minutes","31 Minutes","6 Minutes",
              "Speech","Text","Text or Speech",
              "Formal","Informal","Formal or Informal")

#####################
### Baseline Results#
#####################

baseline_citizens <- lapply(ats,function(x){
  as.data.frame(
    marginaleffects::predictions(
      svyglm(as.formula(paste("selected~",paste0(ats,collapse="+"))),
             design=svydesign(ids=~ResponseId,data=citizen)),
      by=x,
      vcov=~ResponseId)) %>%
    rename(Level=x)}) %>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))

baseline_municipalities <- lapply(ats,function(x){
  as.data.frame(
    marginaleffects::predictions(
      svyglm(as.formula(paste("selected~",paste0(ats,collapse="+"))),
             design=svydesign(ids=~ResponseId,data=municipalities)),
      by=x,
      vcov=~ResponseId)) %>%
    rename(Level=x)}) %>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))

# Figure 1: Baseline Results Citizen Sample

mm_base <- ggplot(baseline_citizens %>%
         mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                Level=fct_relevel(Level,"Including Discretion",after=11),
                Level=fct_relevel(Level,"14 %",after=13),
                Level=fct_relevel(Level,"17 Minutes",after=16),
                Level=fct_relevel(Level,"Saved with consent",after=4)
                ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0.5)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high),
                  position=position_dodge2(width=0.5),
                  color="navy")+
  facet_grid(Attribute~"Baseline Results\n(Citizen Sample)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(0,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA))+
  labs(y="Marginal Means",x="")
mm_base
ggsave(mm_base,filename="Figure1.PNG",width=7,height=6,dpi=400)

# Figure A1: Baseline Results Municipalities

base_mun <- ggplot(baseline_municipalities %>%
         mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                Level=fct_relevel(Level,"Including Discretion",after=11),
                Level=fct_relevel(Level,"14 %",after=13),
                Level=fct_relevel(Level,"17 Minutes",after=16),
                Level=fct_relevel(Level,"Saved with consent",after=4)
         ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0.5)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high),
                  position=position_dodge2(width=0.5),
                  color="darkorange",shape=17)+
  facet_grid(Attribute~"Baseline Results\n(Municipal Sample)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(0,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA))+
  labs(y="Marginal Means",x="")
base_mun
ggsave(base_mun,filename="FigureA6.PNG",width=7,height=6,dpi=400)


##########################
### Sample Differences ###
##########################

### Citizen Sample vs. Frontline Workers

mm_samples <- lapply(ats,function(x){
  as.data.frame(marginaleffects::predictions(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*sample")),
           design=svydesign(ids=~ResponseId,
                            data=bind_rows(
                              select(citizen,c("ResponseId",ats,"selected","sample")),
                              select(municipalities,c("ResponseId",ats,"selected","sample"))
                            ))),
    by=c(x,"sample"),
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))


diff_samples <- lapply(ats,function(x){
  as.data.frame(marginaleffects::comparisons(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*sample")),
           design=svydesign(ids=~ResponseId,
                            data=bind_rows(
                              select(citizen,c("ResponseId",ats,"selected","sample")),
                              select(municipalities,c("ResponseId",ats,"selected","sample"))
                            ))),
    variables="sample",
    by=x,
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name),
         sample=factor(contrast,labels=c("Municipalities")))


# Figure 2: Citizens vs. Frontline Workers

difsam <- ggplot(diff_samples %>%
         mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                Level=fct_relevel(Level,"Including Discretion",after=11),
                Level=fct_relevel(Level,"14 %",after=13),
                Level=fct_relevel(Level,"17 Minutes",after=16),
                Level=fct_relevel(Level,"Saved with consent",after=4)
         ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high),
                  position=position_dodge2(width=0.5),
                  shape=17,color="darkorange")+
  facet_grid(Attribute~"Municipalities vs. Citizens\n(Differences in Marginal Means)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom",
        legend.title=element_blank())+
  labs(y="Marginal Means",x="")
difsam
ggsave(difsam,filename="Figure2.PNG",width=7,height=6,dpi=400)


######################
### Subgroup GAAIS ###
######################

### Code GAAIS Scale
gaaispos <- c("gaais1","gaais2","gaais4","gaais5","gaais7","gaais11","gaais12","gaais13","gaais14","gaais16","gaais17","gaais18")
gaaisneg <- c("gaais3","gaais6","gaais8","gaais9","gaais10","gaais15","gaais19","gaais20")

## Test internal consistency

psych::alpha(citizen[c(gaaisneg)]) # Negative Dimension
psych::alpha(citizen[c(gaaispos)]) # Positive Dimension

# Both Dimensions with reverse scaled positive dimension
psych::alpha(
  data.frame(citizen[c(gaaispos)],
             citizen[c(gaaisneg)]%>%
               mutate(across(all_of(gaaisneg), ~ 7 - .))))

# Predict Factor Scores

psych::fa(data.frame(citizen[c(gaaispos)],
                     citizen[c(gaaisneg)]%>%
                       mutate(across(all_of(gaaisneg), ~ 7 - .))),
          nfactors=1)

citizen$GAAIS <- psych::fa(data.frame(citizen[c(gaaispos)],
             citizen[c(gaaisneg)]%>%
               mutate(across(all_of(gaaisneg), ~ 7 - .))),nfactors=1)$scores

## Bin GAAIS in three terciles

citizen$GAAIS_bin <- factor(cut_number(citizen$GAAIS,3),labels=c("Low","Medium","High"))

## Estimate Conjoint Results by GAAIS

mm_gaais <- lapply(ats,function(x){
  as.data.frame(marginaleffects::predictions(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*GAAIS_bin")),
           design=svydesign(ids=~ResponseId,
                            data=citizen)),
    by=c(x,"GAAIS_bin"),
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))

diff_gaais <- lapply(ats,function(x){
  as.data.frame(marginaleffects::comparisons(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*GAAIS_bin")),
           design=svydesign(ids=~ResponseId,
                            data=citizen)),
    variables="GAAIS_bin",
    by=x,
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name),
         sample=factor(contrast,labels=c("Municipalities")))


# Figure X: Results by GAAIS

gaais <- ggplot(diff_gaais %>%
         mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                Level=fct_relevel(Level,"Including Discretion",after=11),
                Level=fct_relevel(Level,"14 %",after=13),
                Level=fct_relevel(Level,"17 Minutes",after=16),
                Level=fct_relevel(Level,"Saved with consent",after=4),
                contrast=fct_relevel(factor(contrast,labels=c("High","Medium")),
                                     "High",after=1)
         ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,color=contrast,shape=contrast,
                      alpha=ifelse((conf.low<0&conf.high<0)|
                                     (conf.low>0&conf.high>0),
                                   "sig","notsig")
                      ),
                  position=position_dodge2(width=0.5))+
  facet_grid(Attribute~"Results by Positive Attitudes towards AI\n(Citizen Sample)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom")+
  labs(y="Difference in MM",x="")+
  scale_color_manual(values=c("darkorange","navy"))+
  scale_alpha_manual(values=c(0.25,1))+
  scale_shape_manual(values=c(15,17))+
  guides(color=guide_legend("Positive Attitudes towards AI\n(GAAIS Index)"),
         shape=guide_legend("Positive Attitudes towards AI\n(GAAIS Index)"),
         alpha="none")
gaais
ggsave(gaais,filename="Figure4.PNG",width=7,height=6,dpi=400)


##############################
### Subgroup AI Regulation ###
##############################

citizen$AIreg <- factor(cut_number(as.numeric(citizen$tech_regulation_1),3),labels=c("Low Support","Medium Support","Strong Support"))

## Estimate Conjoint Results by Support for stricter AI Regulation

mm_AI <- lapply(ats,function(x){
  as.data.frame(marginaleffects::predictions(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*AIreg")),
           design=svydesign(ids=~ResponseId,
                            data=citizen)),
    by=c(x,"AIreg"),
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))

diff_AI <- lapply(ats,function(x){
  as.data.frame(marginaleffects::comparisons(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*AIreg")),
           design=svydesign(ids=~ResponseId,
                            data=citizen)),
    variables="AIreg",
    by=x,
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name),
         sample=factor(contrast,labels=c("Municipalities")))


# Figure X: Results by Support for stricter Regulation

AIreg <- ggplot(diff_AI %>%
                  mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                         Level=fct_relevel(Level,"Including Discretion",after=11),
                         Level=fct_relevel(Level,"14 %",after=13),
                         Level=fct_relevel(Level,"17 Minutes",after=16),
                         Level=fct_relevel(Level,"Saved with consent",after=4),
                         contrast=factor(contrast,labels=c("Medium Support","Strong Support"))
                  ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,color=contrast,shape=contrast,
                      alpha=ifelse((conf.low<0&conf.high<0)|
                                     (conf.low>0&conf.high>0),
                                   "sig","notsig")
  ),
  position=position_dodge2(width=0.5))+
  facet_grid(Attribute~"Results by Support for Strict Regulation of AI\n(Citizen Sample)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom")+
  labs(y="Difference in MM",x="")+
  scale_color_manual(values=c("darkorange","navy"))+
  scale_alpha_manual(values=c(0.25,1))+
  scale_shape_manual(values=c(15,17))+
  guides(color=guide_legend("Support for Strict Regulation of AI\n(vs. Low Support)"),
         shape=guide_legend("Support for Strict Regulation of AI\n(vs. Low Support)"),
         alpha="none")
AIreg
ggsave(AIreg,filename="Figure5.PNG",width=7,height=6,dpi=400)

######################
### Priming Effect ###
######################

mm_priming <- lapply(ats,function(x){
  as.data.frame(marginaleffects::predictions(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*Prime")),
           design=svydesign(ids=~ResponseId,
                            data=citizen
                            )),
    by=c(x,"Prime"),
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name))

diff_priming <- lapply(ats,function(x){
  as.data.frame(marginaleffects::comparisons(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*Prime")),
           design=svydesign(ids=~ResponseId,
                            data=citizen
                            )),
    variables="Prime",
    by=x,
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name),
         Prime=factor(contrast,labels=c("Professional","Efficiency")))

# Figure X: Priming Effect

prime <- ggplot(diff_priming %>%
         mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                Level=fct_relevel(Level,"Including Discretion",after=11),
                Level=fct_relevel(Level,"14 %",after=13),
                Level=fct_relevel(Level,"17 Minutes",after=16),
                Level=fct_relevel(Level,"Saved with consent",after=4),
                sig=ifelse((conf.low<0&conf.high<0)|(conf.low>0&conf.high>0),"sig","notsig")
         ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,shape=Prime,color=Prime,alpha=sig),
                  position=position_dodge2(width=0.5))+
  facet_grid(Attribute~"Priming Effect vs. Control",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom",
        legend.title=element_blank())+
  labs(y="Treatment Effect",x="")+
  scale_color_manual(values=c("red","blue"))+
  scale_shape_manual(values=c(15,17))+
  scale_alpha_manual(values=c(0.25,1))+
  guides(alpha="none")
prime
ggsave(prime,filename="Figure3.PNG",width=7,height=6,dpi=400)

########################################
### Attribute Interaction: Efficiency ##
########################################

ame_eff <- merge(
  bind_rows(setNames(lapply(ats[ats!="Time"],function(x){
    as.data.frame(marginaleffects::comparisons(
      svyglm(as.formula(paste("selected~(",
                              paste0(ats[ats!="Time"],collapse="+"),
                              ")*Time")),
             design=svydesign(ids=~ResponseId,
                              data=citizen %>%
                                mutate(
                                  Development = relevel(factor(Development), ref = "ausl�ndisches IT-Unternehmen"),
                                  Privacy = relevel(factor(Privacy), ref = "mit Einverst�ndnis gespeichert"),
                                  Takeover = relevel(factor(Takeover), ref = "Anzeige E-Mail Adresse des B�rgeramtes"),
                                  ADM = relevel(factor(ADM), ref = "niemals �ber Antr�ge"),
                                  Time = relevel(factor(Time), ref = "6 Minuten"),
                                  Workload = relevel(factor(Workload), ref = "4%"),
                                  Interface = relevel(factor(Interface), ref = "Texteingabe"),
                                  Style = relevel(factor(Style), ref = "amtliche Sprache (Sie)")
                                )
                              )),
      variables=paste(x),
      by=c("Time"),
      vcov=~ResponseId))%>%
      mutate(contrast = factor(contrast,labels=levels(citizen[[paste(x)]])[-1]))
  }),ats[ats!="Time"]),.id="Attribute"),
  bind_rows(lapply(ats[ats!="Time"],function(x){
    expand.grid(paste(x),
                levels(citizen[[paste(x)]]),
                levels(citizen$Time))
  }))%>%
    rename(Attribute=1,contrast=2,Time=3),
  all=T) %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats[-c(6)]),
         Attribute = factor(Attribute,labels=at_name[-c(6)]),
         contrast = factor(contrast, levels = levels(contrast)),
         contrast = recode(
           contrast,
           "deutsches IT-Unternehmen" = "National Company",
           "kommunales IT-Unternehmen" = "Local Company",
           "ausl�ndisches IT-Unternehmen" = "Foreign Company",
           "automatisch gespeichert" = "Always Saved",
           "mit Einverst�ndnis gespeichert" = "Saved with consent",
           "automatisch gel�scht" = "Always Deleted",
           "niemals �ber Antr�ge" = "Never",
           "nur Antr�ge ohne Beurteilungsspielraum" = "Excluding Discretion",
           "Antr�ge mit und ohne Beurteilungsspielraum" = "Including Discretion",
           "Anzeige E-Mail Adresse Sachbearbeiter:in" = "Mail Contact (Frontline Worker)",
           "Anzeige E-Mail Adresse des B�rgeramtes" = "Mail Contact (Municipality)",
           "Sachbearbeiter:in �bernimmt Chat" = "Clerk takes over",
           "Texteingabe" = "Text",
           "Spracheingabe" = "Speech",
           "w�hlbar (Text/Sprache)" = "Text or Speech",
           "amtliche Sprache (Sie)" = "Formal",
           "nat�rliche Sprache (Du)" = "Informal",
           "w�hlbar (amtlich/nat�rlich)" = "Formal or Informal")
  )

ame_eff[is.na(ame_eff)] <- 0


ame_eff2 <- merge(bind_rows(setNames(lapply(ats[ats!="Workload"],function(x){
  as.data.frame(marginaleffects::comparisons(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats[ats!="Workload"],collapse="+"),
                            ")*Workload")),
           design=svydesign(ids=~ResponseId,
                            data=municipalities%>%
                              mutate(
                                Development = relevel(factor(Development), ref = "ausl�ndisches IT-Unternehmen"),
                                Privacy = relevel(factor(Privacy), ref = "mit Einverst�ndnis gespeichert"),
                                Takeover = relevel(factor(Takeover), ref = "Anzeige E-Mail Adresse des B�rgeramtes"),
                                ADM = relevel(factor(ADM), ref = "niemals �ber Antr�ge"),
                                Time = relevel(factor(Time), ref = "6 Minuten"),
                                Workload = relevel(factor(Workload), ref = "4%"),
                                Interface = relevel(factor(Interface), ref = "Texteingabe"),
                                Style = relevel(factor(Style), ref = "amtliche Sprache (Sie)")
                              )
           )),
    variables=paste(x),
    by=c("Workload"),
    vcov=~ResponseId))%>%
    mutate(contrast = factor(contrast,labels=levels(municipalities[[paste(x)]])[-1]))
}),ats[ats!="Workload"]),.id="Attribute"),
bind_rows(lapply(ats[ats!="Workload"],function(x){
  expand.grid(paste(x),
              levels(municipalities[[paste(x)]]),
              levels(municipalities$Workload))
}))%>%rename(Attribute=1,contrast=2,Workload=3),
all=T) %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats[-c(5)]),
         Attribute = factor(Attribute,labels=at_name[-c(5)]),
         contrast = factor(contrast, levels = levels(contrast)),
         contrast = recode(
           contrast,
           "deutsches IT-Unternehmen" = "National Company",
           "kommunales IT-Unternehmen" = "Local Company",
           "ausl�ndisches IT-Unternehmen" = "Foreign Company",
           "automatisch gespeichert" = "Always Saved",
           "mit Einverst�ndnis gespeichert" = "Saved with consent",
           "automatisch gel�scht" = "Always Deleted",
           "niemals �ber Antr�ge" = "Never",
           "nur Antr�ge ohne Beurteilungsspielraum" = "Excluding Discretion",
           "Antr�ge mit und ohne Beurteilungsspielraum" = "Including Discretion",
           "Anzeige E-Mail Adresse Sachbearbeiter:in" = "Mail Contact (Frontline Worker)",
           "Anzeige E-Mail Adresse des B�rgeramtes" = "Mail Contact (Municipality)",
           "Sachbearbeiter:in �bernimmt Chat" = "Clerk takes over",
           "Texteingabe" = "Text",
           "Spracheingabe" = "Speech",
           "w�hlbar (Text/Sprache)" = "Text or Speech",
           "amtliche Sprache (Sie)" = "Formal",
           "nat�rliche Sprache (Du)" = "Informal",
           "w�hlbar (amtlich/nat�rlich)" = "Formal or Informal")
  )

ame_eff2[is.na(ame_eff2)] <- 0

int_cit <- ggplot(ame_eff ,aes(x=fct_rev(contrast),y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,shape=Time,color=Time),
                  position=position_dodge2(width=0.5))+
  facet_grid(Attribute~"Results by Efficiency Increase\n(Citizens)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom",
        legend.title=element_blank())+
  labs(y="Average Marginal Component Effect",x="")+
  scale_color_manual(values=c("darkred","blue","navy"))+
  scale_shape_manual(values=c(16,18,17))

int_mun <- ggplot(ame_eff2 ,aes(x=fct_rev(contrast),y=estimate))+
  geom_hline(yintercept=0)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,shape=Workload,color=Workload),
                  position=position_dodge2(width=0.5))+
  facet_grid(Attribute~"Results by Efficiency Increase\n(Frontline Workers)",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom",
        legend.title=element_blank())+
  labs(y="Average Marginal Component Effect",x="")+
  scale_color_manual(values=c("darkred","blue","navy"))+
  scale_shape_manual(values=c(16,15,17))

int_cit
int_mun

ggsave(int_cit,file="FigureA7.PNG",dpi=400,width=7,height=6)
ggsave(int_mun,file="FigureA8.PNG",dpi=400,width=7,height=6)


####################
### Descriptives ###
####################

citizen$education <- as.factor(citizen$education)
municipalities$education <- as.factor(municipalities$education)

levels(citizen$education) <- c("Without Degree",
                          "Student",
                          "Hauptschule\n(Lower Secondary Degree)",
                          "Realschule\n(Intermediate Degree)",
                          "(Fach-)Abitur\n(High School Degree)",
                          "(Applied) University Degree")


levels(municipalities$education) <- c("Hauptschule\n(Lower Secondary Degree)",
                                      "Without Degree",
                                      "Student",
                                      "Realschule\n(Intermediate Degree)",
                                      "(Fach-)Abitur\n(High School Degree)",
                                      "(Applied) University Degree")

desc <- cowplot::plot_grid(
ggplot(data.frame(
  bind_rows(select(citizen,c(gender,sample)),
            select(municipalities,c(gender,sample))
            ))%>%
    group_by(sample,gender) %>%
    summarize(n=n()) %>%
    mutate(freq=n/sum(n)),
    aes(x=gender))+
  geom_col(aes(y = freq*100,fill=sample,color=sample),
           position=position_dodge2(width=0.5)) +
  geom_text(aes(y=(freq*100)+1,label=paste(round(freq*100,2),"%")),
            position=position_dodge2(width=1),color="grey",
            size=3)+
  facet_wrap(.~"Gender",)+
  labs(x="",
       y="Share of respondents")+
  scale_color_manual(values=c("navy","orange"),
                     labels=c(paste("Citizens"),
                              paste("Municipalities")))+
  scale_fill_manual(values=c("navy","orange"),
                    labels=c(paste("Citizens"),
                             paste("Municipalities")))+
  scale_x_discrete(labels=c("Non-Binary","Female","Male"),limits=rev)+
  theme_classic()+
  theme(legend.position = "bottom",
        legend.title=element_blank(),
        legend.background = element_blank()),
ggplot(data.frame(
  bind_rows(select(citizen,c(education,sample)),
            select(municipalities,c(education,sample))
  ))%>%
    group_by(sample,education) %>%
    summarize(n=n()) %>%
    mutate(freq=n/sum(n)),
  aes(x=education))+
  geom_col(aes(y = freq*100,fill=sample),
           position=position_dodge2(width=0.5),color="black") +
  geom_text(aes(y=(freq*100)+1,label=paste(round(freq*100,2),"%")),
            position=position_dodge2(width=1),color="grey",
            size=3)+
  facet_wrap(.~"Education",)+
  labs(x="",
       y="Share of respondents")+
  scale_fill_manual(values=c("navy","orange"),
                    labels=c(paste("Citizens"),
                             paste("Municipalities")))+
  scale_x_discrete()+
  theme_classic()+
  theme(legend.position = "bottom",
        legend.title=element_blank(),
        legend.background = element_blank())+
  coord_flip(),
ggplot(bind_rows(citizen[c("age","sample")],
                 municipalities[c("age","sample")]),
       aes(x=as.numeric(age),fill=sample),
       color="black")+
  geom_density(bw=5,alpha=0.65,linewidth=0.5)+
  theme_classic()+
  theme(legend.position = "bottom")+
  scale_fill_manual(labels=c(
    paste("Citizens\n(M=",round(mean(as.numeric(citizen$age)),2),")"),
    paste("Municipalities\n(M=",
          round(mean(as.numeric(
            municipalities$age[!is.na(as.numeric(municipalities$age))])),2),
          ")")),
    values=c("navy","darkorange"))+
  labs(x="")+
  facet_wrap("Age"~.,),
ggplot(citizen,aes(x=GAAIS))+
  geom_density(alpha=0.65,color="black",aes(fill=sample))+
  theme_classic()+
  facet_wrap("Positive Attitudes towards AI\n(GAAIS Index)"~.,)+
  scale_fill_manual(labels=c(paste("Citizens\n(M=",round(mean(citizen$GAAIS)),2,")")),
                    values=c("navy"))+
  theme(legend.position="bottom")
)
desc
ggsave(desc,filename="FigureA1.PNG",width=9,height=7,dpi=400)


contacts <- contacts %>%
  left_join(.,municipalities %>% filter(task == 1 & profile == 1))

contacts <- bind_rows(contacts,contacts%>%filter(task==1&profile==1),.id="Sample") %>%
  mutate(.,
         Sample = ifelse(Sample == 1,0,1),
         GGK = as.factor(case_when(
           Einwohnerzahl < 2000 ~ "< 2.000",
           Einwohnerzahl > 1999 &  Einwohnerzahl < 5000 ~ "2-4.999",
           Einwohnerzahl > 4999 &  Einwohnerzahl < 20000 ~ "5-19.999",
           Einwohnerzahl > 19999 & Einwohnerzahl < 50000 ~ "20-49.999",
           Einwohnerzahl > 49999 & Einwohnerzahl < 100000 ~ "50-99.999",
           Einwohnerzahl > 99999  ~ ">100.000")),
         Party =  case_when(
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"SPD") ~ "SPD",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"CDU") ~ "CDU/CSU",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"CSU") ~ "CDU/CSU",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"FDP") ~ "FDP",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"Gr ") ~ "Gr�ne",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"Lin") ~ "Linke",
             str_detect(`B�rgermeister - Partei / W�hlergruppe`,"los") ~ "Independent"),
           Party = as.factor(ifelse(is.na(Party),"Local",Party)),
         Party = fct_relevel(Party,"SPD",after=1)
         ) %>%
  rename(.,
         density ="Bev�lkerungsdichte (Einw. je km2)",
         State = factor("Bundesland"))

desc_mun <- cowplot::plot_grid(
  left_join(
  left_join(expand_grid(levels(as.factor(contacts$State)),unique(contacts$Sample)) %>%
              rename(.,State=1,Sample=2),
            contacts %>% group_by(Sample,State) %>%
              summarise(.,n=n()) %>%
              mutate(.,prop = n/sum(n))),
  summary(marginaleffects::marginaleffects(multinom(State ~ Sample,data=contacts))) %>%
    rename(.,State=group,Sample = term) %>% mutate(.,Sample =1)) %>%
  mutate(.,
         Sample = ifelse(Sample == 1,"Sample","Population")) %>%
  ggplot(.,aes(x=fct_rev(State)))+
  geom_col(aes(y=prop,fill=Sample,group=Sample),
           position="dodge",alpha=0.25)+
  geom_pointrange(aes(y=estimate,ymin=conf.low,ymax=conf.high,group=Sample),
                  position="dodge",color="black")+
  theme_classic()+
  theme(panel.grid.major.x = element_line(linetype=1),
        panel.grid.minor.x = element_line(linetype=2),
        legend.position="bottom",
        legend.title = element_blank())+
  coord_flip()+
  geom_hline(yintercept=0)+
  labs(x="",y="(Difference in)\nProportions of Municipalities")+
  scale_fill_manual(values=c("navy","darkorange"))+
  scale_y_continuous(breaks=seq(-1,1,by=0.05))+
  facet_wrap("State"~.,),
  left_join(
    left_join(expand_grid(levels(as.factor(contacts$GGK)),unique(contacts$Sample)) %>%
                rename(.,GGK=1,Sample=2),
              contacts %>% group_by(Sample,GGK) %>%
                summarise(.,n=n()) %>%
                mutate(.,prop = n/sum(n))),
    summary(marginaleffects::marginaleffects(multinom(GGK ~ Sample,data=contacts))) %>%
      rename(.,GGK=group,Sample = term) %>% mutate(.,Sample =1)) %>%
    mutate(.,Sample = ifelse(Sample == 1,"Sample","Population"),
           GGK=fct_relevel(GGK,">100.000",after=10),
           GGK=fct_relevel(GGK,"5-19.999",after=2)) %>%
    ggplot(.,aes(x=fct_rev(GGK)))+
    geom_col(aes(y=prop,fill=Sample,group=Sample),
             position="dodge",alpha=0.25)+
    geom_pointrange(aes(y=estimate,ymin=conf.low,ymax=conf.high,group=Sample),
                    position="dodge",color="black")+
    theme_classic()+
    theme(panel.grid.major.x = element_line(linetype=1),
          panel.grid.minor.x = element_line(linetype=2),
          legend.position="bottom",
          legend.title = element_blank())+
    coord_flip()+
    geom_hline(yintercept=0)+
    labs(x="",y="(Difference in)\nProportions of Municipalities")+
    scale_fill_manual(values=c("navy","darkorange"))+
    scale_y_continuous(breaks=seq(-1,1,by=0.1))+
    facet_wrap("Population Size"~.,),
  left_join(
    left_join(expand_grid(levels(as.factor(contacts$Party)),unique(contacts$Sample)) %>%
                rename(.,Party=1,Sample=2),
              contacts %>% group_by(Sample,Party) %>%
                summarise(.,n=n()) %>%
                mutate(.,prop = n/sum(n))),
    summary(marginaleffects::marginaleffects(multinom(Party ~ Sample,data=contacts))) %>%
      rename(.,Party=group,Sample = term) %>% mutate(.,Sample =1)) %>%
    mutate(.,Sample = ifelse(Sample == 1,"Sample","Population")) %>%
    ggplot(.,aes(x=fct_rev(Party)))+
    geom_col(aes(y=prop,fill=Sample,group=Sample),
             position="dodge",alpha=0.25)+
    geom_pointrange(aes(y=estimate,ymin=conf.low,ymax=conf.high,group=Sample),
                    position="dodge",color="black")+
    theme_classic()+
    theme(panel.grid.major.x = element_line(linetype=1),
          panel.grid.minor.x = element_line(linetype=2),
          legend.position="bottom",
          legend.title = element_blank())+
    coord_flip()+
    geom_hline(yintercept=0)+
    labs(x="",y="(Difference in)\nProportions of Municipalities")+
    scale_fill_manual(values=c("navy","darkorange"))+
    scale_y_continuous(breaks=seq(-1,1,by=0.1))+
    facet_wrap("Party Affilitation (Mayor)"~.,)
)
desc_mun
ggsave(desc_mun,filename="FigureA3.PNG",width=8,height=8,dpi=400)


################################
#### Location of respondents ###
################################


library(ggmap)

register_stadiamaps("#")

DE <- get_stadiamap(c(4,46.5,16,55.5), zoom=7,maptype="stamen_toner_lite")
municipalities$LocationLatitude <- as.numeric(as.character(municipalities$LocationLatitude))
municipalities$LocationLongitude <- as.numeric(as.character(municipalities$LocationLongitude))
citizen$LocationLatitude <- as.numeric(as.character(citizen$LocationLatitude))
citizen$LocationLongitude <- as.numeric(as.character(citizen$LocationLongitude))

# Frontline Workers
map <- ggmap(DE)+
  geom_point(aes(x=LocationLongitude,y=LocationLatitude,color=sample),
             data=citizen[!is.na(citizen$selected),]%>%
               mutate(sample = "Citizens\n(n=1690)"),
             size=1,
             shape=15)+
  geom_point(aes(x=LocationLongitude,y=LocationLatitude,color=sample),
             data=municipalities[!is.na(municipalities$selected),]%>%
               mutate(sample = "Municipalities\n(n=267)"),
             size=2,
             shape=17)+
  theme_classic()+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        legend.position = "bottom",
        legend.title=element_blank())+
  scale_color_manual(values=c("navy","darkorange"))+
  facet_wrap("Location of Respondents"~.,)+
  labs(x="",y="")
map
ggsave(map,filename="FigureA2.PNG",width=5,height=5,dpi=400)


##################
### Robustness ###
##################

Values <- select(citizen,c("Prime",
                            "prime_manipulation_1",
                            "prime_manipulation_2",
                            "prime_manipulation_3")
                 ) %>%
  pivot_longer(
    cols = 2:4,
    names_to = "importance",
    values_to = "text",
    values_drop_na = TRUE) %>%
  mutate(text = tolower(text),
         Value=NA,
         Value = case_when(
           str_detect(text,"serv|dienst") ~ "Service",
           str_detect(text,"unp") ~ "Impartiality",
           str_detect(text,"�ffn|termi") ~ "Flexibility",
           str_detect(text,"fair|gere") ~ "Fairness",
           str_detect(text,"recht") ~ "Lawfulness",
           str_detect(text,"wohl") ~ "Benovelence",
           str_detect(text,"ehrl|erli|korr") ~ "Honesty",
           str_detect(text,"effi|efi|zeitn") ~ "Efficiency",
           str_detect(text,"effek") ~ "Effective",
           str_detect(text,"ex") ~ "Flexibility",
           str_detect(text,"qual") ~ "Quality",
           str_detect(text,"kund") ~ "Customer-Orientation",
           str_detect(text,"b�rg|burg") ~ "Citizen-Orientation",
           str_detect(text,"freund") ~ "Friendliness",
           str_detect(text,"schn") ~ "Fast",
           str_detect(text,"einfach") ~ "Covenience",
           str_detect(text,"b�r") ~ "Covenience",
           str_detect(text,"helf|hilf") ~ "Supportive",
           str_detect(text,"err") ~ "accessible",
           str_detect(text,"wiss|komp") ~ "Knowledgable",
           str_detect(text,"zufe|zuve") ~ "Reliable"
           ),
         text = ifelse(!is.na(Value),Value,text))



table(Values$text[str_detect(Values$text,"serv|dienst")])


words <- bind_rows(setNames(lapply(
  list(Values$text[Values$Prime=="Service"],
       Values$text[Values$Prime=="Duty"],
       Values$text[Values$Prime=="Control"]),
  function(x){
    sort(rowSums(as.matrix(TermDocumentMatrix(
      Corpus(VectorSource(x))%>%
        tm_map(removeNumbers) %>%
        tm_map(removePunctuation) %>%
        tm_map(stripWhitespace) %>%
        tm_map(., content_transformer(tolower)) %>%
        tm_map(., removeWords, stopwords("german"))
        ))),decreasing=T) %>% data.frame(word=names(.),freq=.)
  }),c("Service","Duty","Control")),.id="Group")%>%
  mutate(word=ifelse(str_detect(word,"wei�|weis|mehr|gut|kurz"),NA,word))

set.seed(1234)

wordcloud(words = words$word[words$Group=="Service"],
          freq = words$freq[words$Group=="Service"],
          max.words=25,
          random.order=F,
          rot.per=0,
          colors=brewer.pal(8, "Dark2"))


wordcloud(words = words$word[words$Group=="Duty"],
            freq = words$freq[words$Group=="Duty"],
            max.words=25,
            random.order=F,
            rot.per=0,
            colors=brewer.pal(8, "Dark2"))

wordcloud(words = words$word[words$Group=="Control"],
          freq = words$freq[words$Group=="Control"],
          max.words=25,
          random.order=F,
          rot.per=0,
          colors=brewer.pal(8, "Dark2"))



wfreq <- ggplot(bind_rows(
  head(words[words$Group=="Control",],15),
  head(words[words$Group=="Service",],15),
  head(words[words$Group=="Duty",],15)
)%>%filter(!is.na(word)),aes(x=reorder(word,freq),y=freq))+
  geom_col(aes(fill=Group),
           position=position_dodge2())+
  theme_classic()+
  scale_fill_manual(values=c("grey","orange","navy"))+
  coord_flip()+
  facet_wrap("Frequency of Values/n(by Treatment Group)"~.,)+
  labs(x="",y="")+
  theme(legend.position = "bottom",
        legend.title = element_blank())
wfreq
ggsave(wfreq,filename="FigureA5.PNG",width=6,height=6,dpi=400)



##########################
#### Robustness Tests ####
##########################

mm_carryover <- lapply(ats,function(x){
  as.data.frame(marginaleffects::predictions(
    svyglm(as.formula(paste("selected~(",
                            paste0(ats,collapse="+"),
                            ")*task")),
           design=svydesign(ids=~ResponseId,
                            data=citizen
           )),
    by=c(x,"task"),
    vcov=~ResponseId)) %>% rename(Level=x)})%>%
  setNames(.,ats) %>%
  bind_rows(.,.id="Attribute") %>%
  mutate(Attribute=ordered(as.factor(Attribute),levels=ats),
         Attribute = factor(Attribute,labels=at_name),
         Level = factor(Level,labels=lvl_name),
         task = as.factor(task))

carryover <- ggplot(mm_carryover %>%
                  mutate(Level=fct_relevel(Level,"Saved with consent",after=4),
                         Level=fct_relevel(Level,"Including Discretion",after=11),
                         Level=fct_relevel(Level,"14 %",after=13),
                         Level=fct_relevel(Level,"17 Minutes",after=16),
                         Level=fct_relevel(Level,"Saved with consent",after=4),
                  ),aes(x=Level,y=estimate))+
  geom_hline(yintercept=0.5)+
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high,shape=task,color=task),
                  position=position_dodge2(width=1),
                  size=0.45)+
  facet_grid(Attribute~"Test for Carryover Effects",scales="free",switch="y")+
  scale_x_discrete(position="top")+
  scale_y_continuous(breaks=seq(0,1,by=0.05))+
  coord_flip()+
  theme_classic()+
  theme(strip.text.y.left = element_text(angle=0,face="bold"),
        panel.grid.major.x = element_line(),
        panel.grid.minor.x = element_line(linetype=2),
        panel.border = element_rect(fill=NA),
        legend.position = "bottom",
        legend.title=element_blank())+
  labs(y="Treatment Effect",x="")+
  scale_color_manual(values=c("navy","navy","purple","purple","orange","orange"))+
  scale_shape_manual(values=c(16,17,16,17,16,17))
carryover
ggsave(carryover,filename="FigureC2.PNG",dpi=400,width=8,height=6)

balancing <- cowplot::plot_grid(
  ggplot(bind_rows(setNames(
    lapply(ats,function(x){
      summary(marginaleffects::comparisons(
        multinom(as.formula(paste(x,"~ gender")),
                 data=citizen%>%
                   mutate(gender=factor(gender,
                                        labels=c("Female",
                                                 "Male",
                                                 "Non-Binary"))
                   )),
        variables="gender"))}),ats),.id="Attribute")%>%
      mutate(contrast=factor(contrast,labels=c("Male","Non-Binary")),
             Attribute=ordered(as.factor(Attribute),levels=ats),
             Attribute = factor(Attribute,labels=at_name),
             group=factor(group,labels=c("14%","17 Minutes","27%","31 Minutes","4%","6 Minutes","Formal",
                              "Including Discretion","Mail Contact (Municipality)",
                              "Mail Contact (Frontline Worker)","Foreign Company","Always deleted",
                              "Always saved","Domestic Company","Local Company","Saved with consent",
                              "Informal","Never","Excluding Discretion","Clerk takes over",
                              "Speech","Text","Formal or Informal","Text or Speech")),
             sig=as.factor(
               ifelse((conf.low<0 & conf.high<0)|
                        (conf.high>0&conf.low>0),1,0))),
    aes(x=group,y=estimate))+
    geom_hline(yintercept=0)+
    theme_classic()+
    geom_pointrange(aes(ymin=conf.low,ymax=conf.high,color=contrast,
                        alpha=sig),
                    position=position_dodge2(width=0.5))+
    facet_grid(Attribute~"Gender\n(vs.Female)",scales="free",switch="y")+
    coord_flip()+
    scale_color_manual(values=c("navy","darkorange"))+
    scale_x_discrete(position="top")+
    scale_alpha_manual(values=c(0.35,1))+
    labs(x="",y="")+
    theme(strip.text.y.left = element_text(angle=0,face="bold"),
          axis.text.y=element_blank(),
          axis.ticks.y.right = element_blank(),
          panel.grid.major.x = element_line(),
          panel.grid.minor.x = element_line(linetype=2),
          panel.border = element_rect(fill=NA),
          legend.position = "bottom",
          legend.title=element_blank())+
    guides(alpha="none"),
  ggplot(bind_rows(setNames(
    lapply(ats,function(x){
      summary(marginaleffects::comparisons(
        multinom(as.formula(paste(x,"~ Agegroup")),
                 data=citizen%>%
                   mutate(Agegroup = factor(cut_number(as.numeric(age),3),
                                            labels=c("18-39","40-60","61-99"))
                          )),
        variables="Agegroup"))}),ats),.id="Attribute")%>%
      mutate(contrast=factor(contrast,labels=c("40-60","61-99")),
             Attribute=ordered(as.factor(Attribute),levels=ats),
             Attribute = factor(Attribute,labels=at_name),
             sig=as.factor(ifelse(
               (conf.low<0 & conf.high<0)|(conf.high>0&conf.low>0),1,0)),
             group=factor(group,labels=c("14%","17 Minutes","27%","31 Minutes","4%","6 Minutes","Formal",
                              "Including Discretion","Mail Contact (Municipality)",
                              "Mail Contact (Frontline Worker)","Foreign Company","Always deleted",
                              "Always saved","Domestic Company","Local Company","Saved with consent",
                              "Informal","Never","Excluding Discretion","Clerk takes over",
                              "Speech","Text","Formal or Informal","Text or Speech"))
             ),
    aes(x=group,y=estimate))+
    theme_classic()+
    geom_hline(yintercept=0)+
    geom_pointrange(aes(ymin=conf.low,ymax=conf.high,color=contrast,alpha=sig),
                    position=position_dodge2(width=0.5))+
    facet_grid(Attribute~"Age\n(vs.18-39)",scales="free",switch="y")+
    coord_flip()+
    scale_color_manual(values=c("navy","darkorange"))+
    scale_x_discrete(position="top")+
    scale_alpha_manual(values=c(0.35,1))+
    labs(x="",y="p(Attribute Level displayed)")+
    theme(strip.text.y.left = element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y.right = element_blank(),
          panel.grid.major.x = element_line(),
          panel.grid.minor.x = element_line(linetype=2),
          panel.border = element_rect(fill=NA),
          legend.position = "bottom",
          legend.title=element_blank())+
    guides(alpha="none"),
  ggplot(bind_rows(setNames(
    lapply(ats,function(x){
      summary(marginaleffects::comparisons(
        multinom(as.formula(paste(x,"~ Education")),
                 data=citizen%>%
                   mutate(
                     Education = factor(education,
                                        labels=c("Low","Low","Low","Medium","High","High"))
                   )),
        variables="Education"))}),ats),.id="Attribute")%>%
      mutate(contrast=factor(contrast,labels=c("Medium","High")),
             Attribute=ordered(as.factor(Attribute),levels=ats),
             Attribute = factor(Attribute,labels=at_name),
             sig=as.factor(ifelse(
               (conf.low<0 & conf.high<0)|(conf.high>0&conf.low>0),1,0)),
             group=factor(group,labels=c("14%","17 Minutes","27%","31 Minutes","4%","6 Minutes","Formal",
                              "Including Discretion","Mail Contact (Municipality)",
                              "Mail Contact (Frontline Worker)","Foreign Company","Always deleted",
                              "Always saved","Domestic Company","Local Company","Saved with consent",
                              "Informal","Never","Excluding Discretion","Clerk takes over",
                              "Speech","Text","Formal or Informal","Text or Speech"))
             ),
    aes(x=group,y=estimate))+
    geom_hline(yintercept=0)+
    theme_classic()+
    geom_pointrange(aes(ymin=conf.low,ymax=conf.high,color=contrast,alpha=sig),
                    position=position_dodge2(width=0.5))+
    facet_grid(Attribute~"Education\n(vs.Low)",scales="free",switch="y")+
    coord_flip()+
    scale_color_manual(values=c("navy","darkorange"))+
    scale_x_discrete(position="top")+
    scale_alpha_manual(values=c(0.35,1))+
    labs(x="",y="")+
    theme(strip.text.y.left = element_blank(),
          panel.grid.major.x = element_line(),
          panel.grid.minor.x = element_line(linetype=2),
          panel.border = element_rect(fill=NA),
          legend.position = "bottom",
          legend.title=element_blank())+
    guides(alpha="none"),
ncol=3,rel_widths=c(1,0.5,1))
balancing
ggsave(balancing,filename="FigureC1.PNG",dpi=400,width=11,height=6)

